package com.cin.cma.server.pos.service.impl;


import com.cin.cma.common.enums.JyServerErrorCode;
import com.cin.cma.common.exception.UserFriendlyException;
import com.cin.cma.common.model.finixx.base.BaseRequest;
import com.cin.cma.common.model.finixx.base.BaseResponse;
import com.cin.cma.common.model.finixx.request.*;
import com.cin.cma.common.model.finixx.response.*;
import com.cin.cma.common.constant.FunctionCode;
import com.cin.cma.server.pos.service.IProxyRequestService;
import com.cin.cma.server.pos.service.ITicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @Date：2024-07-18 14:45
 */
@Slf4j
@Service
public class TicketServiceImpl implements ITicketService {
    @Autowired
    private IProxyRequestService proxyRequestService;

    /**
     * 获取全天排期信息
     *
     * @param model
     * @return
     */
    @Override
    public SessionResponse getSession(SessionRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_SESSIONS);
        return this.proxyRequestService.context(model, SessionResponse.class);
    }

    /**
     * 获取排期影片信息
     *
     * @param model
     * @return
     */
    @Override
    public SessionFilmResponse getSessionFilms(SessionFilmRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_SESSION_FILMS);
        return this.proxyRequestService.context(model, SessionFilmResponse.class);
    }

    /**
     * 获取影厅信息
     *
     * @param model
     * @return
     */
    @Override
    public HallsResponse getHalls(BaseRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_HALLS);
        return this.proxyRequestService.context(model, HallsResponse.class);
    }

    /**
     * 获取场区信息
     *
     * @param model
     * @return
     */
    @Override
    public SectionsResponse getSections(SectionsRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_CINEMA_SECTIONS);
        return this.proxyRequestService.context(model, SectionsResponse.class);
    }

    /**
     * 获取座位图座位信息
     *
     * @param model
     * @return
     */
    @Override
    public SeatPlanResponse getSeatPlan(SectionsRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_SEATPLAN);
        return this.proxyRequestService.context(model, SeatPlanResponse.class);
    }

    /**
     * 获取排期价格政策
     *
     * @param model
     * @return
     */
    @Override
    public SessionPricePolicyResponse getSessionPricePolicy(SessionPricePolicyRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_SESSION_PRICEPOLICY);
        return this.proxyRequestService.context(model, SessionPricePolicyResponse.class);
    }

    /**
     * 获取订单号
     *
     * @param model
     * @return
     */
    @Override
    public BookingIdResponse getBookingId(BaseRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_BOOKING_ID);
        return this.proxyRequestService.context(model, BookingIdResponse.class);
    }

    /**
     * 锁座
     *
     * @param model
     * @return
     */
    @Override
    public RepositionSeatsResponse repositionSeats(RepositionSeatsRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_LOCK_SEATS);
        RepositionSeatsResponse repositionSeatsResponse = this.proxyRequestService.context(model, RepositionSeatsResponse.class, result -> {
            switch (result) {
                case 2004:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_SHOW_DETAILS_NOT_FOUND);
                case 2076:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_SESSION_IN_VAILD);
                case 2075:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_SESSION_CLOSE);
                default:
                    break;
            }
        });
        if (repositionSeatsResponse.getResult() == 2011) {
            StringBuilder errorMsg = new StringBuilder();
            for (SeatItemResponse seat : repositionSeatsResponse.getSeats()) {
                if (!seat.getRepositionSeatSuccess()) {
                    errorMsg.append(seat.getRowId()).append("排").append(seat.getColId()).append("座").append(seat.getRemark());
                }
            }
            throw new UserFriendlyException(JyServerErrorCode.TICKET_LOCK_SEAT_FAIL, errorMsg.toString());
        }
        return repositionSeatsResponse;
    }

    /**
     * 释放座位
     *
     * @param model
     * @return
     */
    @Override
    public RepositionSeatsResponse releaseSeats(RepositionSeatsRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_UNLOCK_SEATS);
        return this.proxyRequestService.context(model, RepositionSeatsResponse.class);
    }

    /**
     * 获取场次已售出或锁定的座位信息
     *
     * @param model
     * @return
     */
    @Override
    public SeatSalesResponse getAllocateSeats(SeatsRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_SESSION_SEATS);
        return this.proxyRequestService.context(model, SeatSalesResponse.class);
    }

    /**
     * 获取订单确认号
     *
     * @param model
     * @return
     */
    @Override
    public ConfirmIdResponse getConfirmId(BaseRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_CONFIRM_ID);
        return this.proxyRequestService.context(model, ConfirmIdResponse.class);
    }

    /**
     * 通过订单号获取订单信息
     *
     * @param model
     * @return
     */
    @Override
    public QueryOrderResponse queryOrder(QueryOrderRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_QUERY_ORDER);
        return this.proxyRequestService.context(model, QueryOrderResponse.class);
    }

    /**
     * 通过取票号获取订单信息
     *
     * @param model
     * @return
     */
    @Override
    public BookingBillResponse getBookingBill(BookingBillRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_BOOKING_BILL);
        return this.proxyRequestService.context(model, BookingBillResponse.class);
    }

    /**
     * 通过订单号获取票支付信息
     *
     * @param model
     * @return
     */
    @Override
    public TicketsResponse getTickets(TicketsRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_TICEKTS_INFO);
        return this.proxyRequestService.context(model, TicketsResponse.class, result -> {
            if (result == 2028) {
                throw new UserFriendlyException(JyServerErrorCode.TICKET_BOOKING_NOT_FOUND);
            }
        });
    }

    /**
     * 退票
     *
     * @param model
     * @return
     */
    @Override
    public BaseResponse refund(RefundRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_REFUND);
        return this.proxyRequestService.context(model, BaseResponse.class, result -> {
            switch (result) {
                case 5079:
                    throw new UserFriendlyException(JyServerErrorCode.POS_ORDER_INTERFACE_ONLY_RETURN);
                case 5002:
                    throw new UserFriendlyException(JyServerErrorCode.POS_NUMBER_IS_NOT_NULL);
                case 6004:
                    throw new UserFriendlyException(JyServerErrorCode.SALES_USER_NOT_FOUND);
                case 6005:
                    throw new UserFriendlyException(JyServerErrorCode.SALES_ROLE_NOT_FOUND);
                case 6006:
                    throw new UserFriendlyException(JyServerErrorCode.SHIFT_OPERATION_DATE_NOT_FOUND);
                case 6010:
                    throw new UserFriendlyException(JyServerErrorCode.SHIFT_OPERATION_DATE_CLOSED);
                case 6007:
                    throw new UserFriendlyException(JyServerErrorCode.SHIFT_DAILY_CASH_FLOAT_NOT_FOUND);
                case 6008:
                    throw new UserFriendlyException(JyServerErrorCode.SHIFT_GET_CASH_FLOAT_FIRST);
                case 6009:
                    throw new UserFriendlyException(JyServerErrorCode.SHIFT_INSERT_DAILY_CASH_FLOAT_FAIL);
                case 2087:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_WRONG_SHIFT_NO_OR_OPRN_DATE);
                case 2028:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_BOOKING_NOT_FOUND);
                case 2030:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_REFUND_REFUND_NONE);
                case 2004:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_SHOW_DETAILS_NOT_FOUND);
                case 2069:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_GET_IBT_FAIL);
                case 2083:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_HAS_REFUNDED);
                case 2026:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_UPDATE_REF_TICKET_FAIL);
                case 2081:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_SEAT_NOT_FOUND);
                case 2106:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_APLLY_TOTAL_NOT_EQUAL_THROUGH_TOTAL);
                case 2100:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_REFUND_NOT_APPLY);
                case 2099:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_UPDATE_REFUND_CONFIRM_FLG_ERROR);
                case 2105:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_UPDATE_INF_REFUND_LCHG_ERROR);
                case 2077:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_PAYMENT_NOT_FOUND);
                case 2024:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_INSERT_REFUND_FAIL);
                case 2025:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_UPDATE_REF_PAYMENT_FAIL);
                case 8002:
                    throw new UserFriendlyException(JyServerErrorCode.MEETING_ARREARAGE_NOT_FOUND);
                case 8000:
                    throw new UserFriendlyException(JyServerErrorCode.MEETING_GET_SALES_FAIL);
                case 2015:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_PAYMENT_TYPE_NOT_FOUND);
                case 6000:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_INSERT_BDKG_FAIL);
                case 2108:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_NOT_REFUND_MULTIPLE_PAY_BOOKING_FEE);
                case 2114:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_INSERT_BDKG_FAIL);
                case 2020:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_INSERT_DAILY_TICKET_FAIL);
                case 2088:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_CALL_CARD_CONSUME_FAIL);
                case 2089:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_CALL_VOUCHER_CONSUME_FAIL);
                default:
                    break;
            }
        });
    }

    /**
     * 获取会员卡价格政策
     *
     * @param model
     * @return
     */
    @Override
    public SessionPricePolicyResponse cardPricePolicy(CardPricePolicyRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_CARD_PRICEPOLICY);
        return this.proxyRequestService.context(model, SessionPricePolicyResponse.class, result -> {
            switch (result) {
                case 3003:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_NOT_FOUND);
                case 2004:
                    throw new UserFriendlyException(JyServerErrorCode.TICKET_SHOW_DETAILS_NOT_FOUND);
            }
        });
    }

    /**
     * 获取影片图片
     *
     * @param model
     * @return
     */
    @Override
    public FilmImageResponse filmImage(FilmImageRequest model) throws Exception {
        model.setCode(FunctionCode.TICKET_GET_FILMS_IMAGE);
        return this.proxyRequestService.context(model, FilmImageResponse.class);
    }

}
